<?php

/**
 * This is the model class for table "{{product}}".
 *
 * The followings are the available columns in table '{{product}}':
 * @property integer $id
 * @property string $name
 * @property string $url
 * @property string $image
 * @property integer $created_at
 * @property string $price
 * @property integer $count_view
 * @property integer $status
 * * @property integer $type
 * @property string $meta_desc
 * @property string $meta_keywords
 * @property string $seo_title
 * @property string $google_index
 */
class Product extends MyARContent
{

    const TYPE_NEW_PROJECT = 1;
    const TYPE_HOT_PROJECT = 2;
    const TYPE_OLD_PROJECT = 0;

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Product the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return string the associated database table name
     */
    public function tableName()
    {
        return '{{product}}';
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            array('name,url,price,c_id', 'required'),
            array('name, url', 'unique', 'on' => 'save'),
            array('created_at,rank,c_id,type,count_view, status', 'numerical', 'integerOnly' => true),
            array('url,price, meta_desc, meta_keywords, seo_title', 'length', 'max' => 250),
            array('image, google_index', 'safe'),
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('id, name ,address, content,description,rank , url,type, image, created_at, price, count_view, status, meta_desc, meta_keywords, seo_title', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations()
    {
        // NOTE: you may need to adjust the relation name and the related
        // class name for the relations automatically generated below.
        $ret                 = parent::relations();
        $ret['productImage'] = array(self::HAS_MANY, 'ProductImage', 'pid');
        $ret['category']     = array(self::BELONGS_TO, 'Category', 'c_id');
        return $ret;
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        $array = array(
            'id'            => 'ID',
            'name'          => 'Name',
            'url'           => 'Url',
            'image'         => 'Image',
            'c_id'          => 'Category',
            'address'       => 'Address',
            'created_at'    => 'Created At',
            'price'         => 'Price',
            'type'          => 'Type',
            'status'        => 'Status',
            'rank'          => 'Rank',
            'description'   => 'Description',
            'content'       => 'Content',
            'meta_desc'     => 'Meta Desc',
            'meta_keywords' => 'Meta Keywords',
            'seo_title'     => 'Seo Title',
            'google_index'     => 'Google',
        );
        foreach ($array as $key => $val)
        {
            $array[$key] = Yii::t('sub_all', $val);
        }

        return $array;
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     * @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
     */
    public function search()
    {
        // Warning: Please modify the following code to remove attributes that
        // should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('name', $this->name, true);
        $criteria->compare('url', $this->url, true);
        $criteria->compare('image', $this->image, true);
        $criteria->compare('created_at', $this->created_at);
        $criteria->compare('price', $this->price);
        $criteria->compare('type', $this->type);
        $criteria->compare('status', $this->status);
        $criteria->compare('meta_desc', $this->meta_desc, true);
        $criteria->compare('meta_keywords', $this->meta_keywords, true);
        $criteria->compare('seo_title', $this->seo_title, true);
        $criteria->compare('google_index', $this->google_index);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    public function getPublicUrl($absolute = false)
    {
        if ($absolute == FALSE)
        {
            return Yii::app()->createUrl('project/view', array(
                        'url' => $this->url,
            ));
        }
        else
        {
            return Yii::app()->createAbsoluteUrl('project/view', array(
                        'url' => $this->url,
            ));
        }
    }

    public function getRegisterUrl($absolute = false)
    {
        if ($absolute == FALSE)
        {
            return Yii::app()->createUrl('project/register', array(
                        'url' => $this->url,
            ));
        }
        else
        {
            return Yii::app()->createAbsoluteUrl('project/register', array(
                        'url' => $this->url,
            ));
        }
    }

    public function getMoney()
    {
        if ($this->price)
        {
            if ($this->special_price == 0)
            {
                return $this->price;
            }
            else
            {
                if ($this->special_price > $this->price)
                {
                    return $this->price;
                }
                else
                {
                    return $this->special_price;
                }
            }
        }
        elseif ($this->special_price)
        {
            if ($this->special_price > $this->price)
            {
                return 0;
            }
            else
            {
                return $this->special_price;
            }
        }
        else
        {
            return 0;
        }
    }

    public function getPriceProduct()
    {

        if ($this->price)
        {
            if ($this->special_price <= 0)
            {
                $price = $this->getNumberFormat($this->price);
                return "Giá: <span class='price'>$price</span>";
            }
            else
            {
                if ($this->special_price > $this->price)
                {

                    $price = $this->getNumberFormat($this->price);
                    return "Giá: <span class='price'>$price</span>";
                }
                else
                {
                    $s_price = $this->getNumberFormat($this->special_price);
                    $price   = $this->getNumberFormat($this->price);
                    return "Giá:<span class='price-sale'>$price</span><span class='price'>$s_price</span>";
                }
            }
        }
        elseif ($this->special_price)
        {
            if ($this->special_price > $this->price or $this->special_price < 0)
            {
                return "Giá: <span class='price'>Liên hệ</span>";
            }
            else
            {
                $s_price = $this->getNumberFormat($this->special_price);
                $price   = $this->getNumberFormat($this->price);
                return "Giá: <span class='price-sale'>$price</span><span class='price-pro'>$s_price</span>";
            }
        }
        else
        {
            return "Giá: <span class='price-sale'>Liên hệ</span>";
        }
    }

    public function getNumberFormat($number)
    {
        return number_format($number, 0, ',', '.') . "VNĐ";
    }

    public function getPublicImage($width = 150, $height = 150)
    {
        if ($this->image)
            return $this->resizePhoto($this->image, $width, $height);
    }

    public static function genRandomString($leng = 10)
    {
        return substr(str_shuffle(str_repeat('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', 5)), 0, $leng);
    }

    public static function getArrayType()
    {
        return array(
            self::TYPE_HOT_PROJECT => 'Dự án hot',
            self::TYPE_NEW_PROJECT => 'Dự án mới',
            self::TYPE_OLD_PROJECT => 'Dự án cũ',
        );
    }

    public function getTitleSeo()
    {
        if ($this->seo_title)
        {
            return $this->seo_title;
        }
        else
        {
            return $this->name;
        }
    }

    /**
     * Lấy các phần tử product thành một mảng
     * 
     */
    public static function getArrayProduct()
    {
        $criteria            = new CDbCriteria;
        $criteria->condition = "status > 0";
        $criteria->params    = array(
        );
        $criteria->order     = 'id DESC';
        $allPublic           = self::model()->findAll($criteria);
        if ($allPublic != NULL)
        {
            foreach ($allPublic as $item)
            {
                $array[$item->id] = $item->name;
            }
        }
        if ($array)
        {
            return $array;
        }
        else
        {
            return array();
        }
    }

}
